<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name" class="input">
        <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" class="btn" size="mini" v-hasPermi="['pet:class:add']"
          @click="handleAdd">新增分类</el-button>
        <el-button type="primary" class="btn" size="mini" v-hasPermi="['pet:class:add']"
          @click="handleAddSub">新增子分类</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="classList" row-key="id"
      :tree-props="{ children: 'list', hasChildren: 'hasChildren' }">
      <el-table-column label="分类" align="center" prop="name" />
      <!-- <el-table-column label="图标" align="center" prop="icon" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pet:class:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pet:class:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改分类管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body class="center">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="input">
        <el-form-item label="分类名称" prop="name" v-if="title == '新增分类' || form.pid == 0">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <div v-if="title == '新增子分类' || form.pid > 0">
          <el-form-item label="子分类名" prop="name">
            <el-input v-model="form.name" placeholder="请输入子分类名称（建议 5 字以内）" />
          </el-form-item>
          <el-form-item label="所属分类" prop="name">
            <el-select v-model="form.pid" placeholder="请选择所属分类" style="width:100%">
              <el-option v-for="item in serveOne" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!-- 图标库 -->
          <!-- <icons :title="iconTitle"></icons> -->
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="btn" style="padding:12px 70px" type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getClass, delClass, addClass, updateClass } from "@/api/pet/class";
import { getServeList, getListServeOne } from "@/api/pet/info";
import icons from "@/components/icons/index"
export default {
  name: "Class",
  components: { icons },
  data() {
    return {
      iconTitle: '分类图标',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类管理表格数据
      classList: [],
      serveOne: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        icon: null,
        pid: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
    getListServeOne().then(res => {
      if (res.code == 200) {
        this.serveOne = res.data
      }
    })
  },
  methods: {
    /** 查询分类管理列表 */
    getList() {
      this.loading = true;
      getServeList(this.queryParams).then(response => {
        this.classList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        icon: null,
        pid: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.pid=0
      this.open = true;
      this.title = "新增分类";
    },
    // 新增子分类
    handleAddSub() {
      this.reset();
      this.open = true;
      this.title = "新增子分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClass(id).then(response => {
        this.form = response.data;
        this.form.pid = Number(this.form.pid)
        this.open = true;
        this.title = "修改分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClass(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.title == '新增分类') {
              this.form.pid = 0
            }
            addClass(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分类管理编号为"' + ids + '"的数据项？').then(function () {
        return delClass(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/class/export', {
        ...this.queryParams
      }, `class_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>